# # -*- coding:utf-8 -*-
# # class TreeNode:
# #     def __init__(self, x):
# #         self.val = x
# #         self.left = None
# #         self.right = None
# class Solution:
#     def isSymmetrical(self, pRoot):
#         # write code here
#         if not pRoot:
#             return False
#         if pRoot.left and pRoot.right:
#             if pRoot.left.val != pRoot.right.val:
#                 return False
#             else:
#                 return self.helper(pRoot.left, pRoot.right)
#         elif not (pRoot.left or pRoot.right):
#             return True
#         else:
#             return False
#
#     def helper(self, pNode_l, pNode_r):
#         if pNode_l.val != pNode_r.val:
#             return False
#         if not (pNode_l.left or pNode_l.right or pNode_r.left or pNode_r.right):
#             return True
#         if pNode_l.left and pNode_r.right:
#
#             if pNode_l.left.val == pNode_r.right.val:
#                 left_flag = self.helper(pNode_l.left, pNode_r.right)
#             else:
#                 left_flag = False
#         elif not (pNode_l.left or pNode_r.right):
#             left_flag = True
#         else:
#             left_flag = False
#
#         if pNode_l.right and pNode_r.left:
#
#             if pNode_l.right.val == pNode_r.left.val:
#                 right_flag = self.helper(pNode_l.right, pNode_r.left)
#             else:
#                 right_flag = False
#         elif not (pNode_l.right or pNode_r.left):
#             right_flag = True
#         else:
#             right_flag = False
#         if left_flag and right_flag:
#             return True
#         else:
#             return False


class Solution:
    def isSymmetrical(self, pRoot):
        if not pRoot:
            return True
        return self.compare(pRoot.left, pRoot.right)
    def compare(self, pRoot1, pRoot2):
        if not pRoot1 and not pRoot2:
            return True
        if not pRoot1 or not pRoot2:
            return False
        if pRoot1.val == pRoot2.val:
            if self.compare(pRoot1.left, pRoot2.right) and self.compare(pRoot1.right, pRoot2.left):
                return True
        return False